- Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSolution.rs
87 lines (77 loc) · 2.15 KB
/
Solution.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
structMyCircularQueue{
data:Vec<i32>,
size:usize,
len:usize,
head:usize,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
implMyCircularQueue{
/** Initialize your data structure here. Set the size of the queue to be k. */
fnnew(k:i32) -> Self{
let k = k asusize;
Self{
data:vec![0; k],
size: k,
len:0,
head:0,
}
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
fnen_queue(&mutself,value:i32) -> bool{
ifself.is_full(){
false
}else{
self.data[(self.head + self.len) % self.size] = value;
self.len += 1;
true
}
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
fnde_queue(&mutself) -> bool{
ifself.is_empty(){
false
}else{
self.head += 1;
self.head %= self.size;
self.len -= 1;
true
}
}
/** Get the front item from the queue. */
fnfront(&self) -> i32{
ifself.is_empty(){
-1
}else{
self.data[self.head]
}
}
/** Get the last item from the queue. */
fnrear(&self) -> i32{
ifself.is_empty(){
-1
}else{
self.data[(self.head + self.len - 1) % self.size]
}
}
/** Checks whether the circular queue is empty or not. */
fnis_empty(&self) -> bool{
self.len == 0
}
/** Checks whether the circular queue is full or not. */
fnis_full(&self) -> bool{
self.len == self.size
}
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* let obj = MyCircularQueue::new(k);
* let ret_1: bool = obj.en_queue(value);
* let ret_2: bool = obj.de_queue();
* let ret_3: i32 = obj.front();
* let ret_4: i32 = obj.rear();
* let ret_5: bool = obj.is_empty();
* let ret_6: bool = obj.is_full();
*/